/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.schnipsel.swt.editor.basis;

import org.eclipse.swt.custom.ExtendedModifyEvent;
import org.eclipse.swt.custom.ExtendedModifyListener;
import org.eclipse.swt.custom.StyleRange;

import de.mnit.basis.daten.struktur.S_Folge;
import de.mnit.basis.daten.struktur.gruppe.Gruppe3;
import de.mnit.basis.daten.struktur.tabelle.typ.TypTabelle3;
import de.mnit.basis.swt.element.steuer.rollbar.gruppe.bild.SWT_TextEditor;
import de.mnit.basis.swt.vorlage.A_Zurueck;
import de.mnit.basis.sys.event.S_Verarbeite;

/**
 * @author Michael Nitsche
 * 21.06.2010	Erstellt
 */
public class TE_Zurueck extends A_Zurueck<Gruppe3<String,StyleRange[],Integer>> {

	private TypTabelle3<String,StyleRange[],Integer> zurueck;
	private SWT_TextEditor te;
	private int[] puffer=new int[] {0};


	public TE_Zurueck(final SWT_TextEditor te) {
		super();
		this.te=te;
		this.te.wennErzeugt(new S_Verarbeite() {
			public void verarbeite() {
				te.swt().addExtendedModifyListener(new ExtendedModifyListener() {
					public void modifyText(ExtendedModifyEvent event) {
						puffer[0]=event.start;
					}
				});
			}
		});
	}


	protected S_Folge<Gruppe3<String, StyleRange[], Integer>> pNeu() {
		return this.zurueck=TypTabelle3.neu(String.class, StyleRange[].class, Integer.class);
	}

	protected void pMinusLetzten() {
		zurueck.minusLetzten();
	}

	protected boolean pPlus() {
		this.puffer=new int[] {te.swt().getCaretOffset()};
		this.zurueck.plus(
			te.gText(),
			te.swt().getStyleRanges(),
			puffer[0]
		);
		return true;
	}

	protected void pSetze(int pos) {
		Gruppe3<String,StyleRange[],Integer> g=this.zurueck.gib(pos);
		te.sText(g.g1());
		te.swt().setStyleRanges(g.g2());
		te.swt().setCaretOffset(g.g3());
	}
}
